# 作者：陈睿-cruise
# 2025年02月09日15时39分06秒
# 2979386061@qq.com
students=[]
def menu():
    print('-' * 40)
    print('欢迎使用学生管理系统')
    print('【1】添加学生信息')
    print('【2】删除学生信息')
    print('【3】修改学生信息')
    print('【4】查询学生信息')
    print('【5】遍历学生信息')
    print('【0】退出系统')
    print('-' * 40)

def add_student():
    student={}
    student['name']=input('请输入学生的姓名')
    student['age'] = input('请输入学生的年龄')
    student['mobile'] = input('请输入学生的电话')
    students.append(student)
    print('添加成功')

def del_student():
    name = input('请输入要删除的同学姓名：')
    for i in students:
        if i['name'] == name:
            students.remove(i)
            print('删除成功')
            break
        else:
            print('查询人员不存在')

def edit_student():
    name=input('请输入要修改的同学姓名：')
    for i in students:
        if i['name'] == name:
            i['name'] = input('请输入学生的姓名')
            i['age'] = int(('请输入学生的年龄'))
            i['mobile'] = input('请输入学生的电话')
            print('修改成功！')
            break
        else:
            print('查询人员不存在')
def find_student():
    name=input('请输入要查找的同学姓名：')
    for i in students:
        if  i['name']==name:
            print(i)
            break
        else:
            print('查询人员不存在')
def save_data_to_file():
    f=open('students.txt','w',encoding='utf-8')
    f.write(str(students))
    print('文件保存完成')
    f.close()

def load_data_to_file():
    f=open('students.txt','r',encoding='utf-8')
    content=f.read()
    if not content:
        return
    global students
    students=eval(content)
    print('载入成功')
    f.close()

if __name__ == '__main__':
    load_data_to_file()
    while  True:
        menu()
        user_num=int(input('请输入您要操作的功能编号'))
        if user_num==1:
            add_student()
        elif user_num==2:
            del_student()
        elif user_num==3:
            edit_student()
        elif user_num==4:
            find_student()
        elif user_num==5:
            print(students)
        elif user_num == 6:
            save_data_to_file()
        elif user_num==0:
            print('退出系统成功，感谢使用')
            break
